import serial
from struct import *
import time

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow
from test_gui import Ui_MainWindow

app = QApplication(sys.argv)
wnd = QMainWindow()
uiDesign = Ui_MainWindow()
uiDesign.setupUi(wnd)

uiDesign.pushButton.clicked.connect(lambda: DoWork())

# 扫描可用串口
from serial.tools.list_ports import comports
for port in comports():
    uiDesign.cmbPorts.addItem(port.name)
    # print("port.name: ",port.name)

def DoWork():
    uiDesign.pushButton.setText('测试中')
    uiDesign.pushButton.setEnabled(False)
    QApplication.processEvents()
    serName = uiDesign.cmbPorts.currentText()
    ser = serial.Serial(serName) # 改为你虚拟出来的一对串口中的其中一个
    import csv
    with open("..csv_fund_quotes\q_217022_债券型-混合债_招商产业债券A.csv", "rt",encoding='utf-8') as csvfile:
        reader3 = csv.reader(csvfile)
        column3=[row[2] for row in reader3]
        #column7=[float(x) for x in column7]
    with open("..csv_fund_quotes\q_217022_债券型-混合债_招商产业债券A.csv", "rt",encoding='utf-8') as csvfile:
        reader4 = csv.reader(csvfile)
        column4=[row[3] for row in reader4]
        #column8=[float(x) for x in column8]
    with open("..csv_fund_quotes\q_217022_债券型-混合债_招商产业债券A.csv", "rt",encoding='utf-8') as csvfile:
        reader5 = csv.reader(csvfile)
        column5=[row[4] for row in reader5]
        #column9=[float(x) for x in column9]
     
    # x = np.arange(0, 2*math.pi, 0.02)
    # y = np.arange(0, 8, 2)
    # print("np.arange: ",y)
    for cyc in range(3):
        for i,j,k in zip(range(1,len(column3)),range(1,len(column4)),range(1,len(column5))):
            y1 = float(column3[i])
            # print(y1)
            y2 = float(column4[j])
            y3 = float(column5[k])
            #y4 = float(0)
            #y5 = float(1)
            #y6 = float(-1)
            dat = pack('fffBBBB', y1, y2, y3, 0x00, 0x00, 0x80, 0x7F)
            ser.write(dat)
            #break
            time.sleep(0.001)
    ser.close()
    uiDesign.pushButton.setText('测试')
    uiDesign.pushButton.setEnabled(True)

wnd.show()
app.exec_()


